const Node = function(_value) {
    this.value = _value;
    this.next = null;
}

const link_list = function() {
    this.head = new Node('head_node');
    /**
     * 查找链表指定value值的节点
     * @param {string} _elm 查找节点的value指
     * 
     */
    this.find = function(_elm) {
        let current_node = this.head;
        while(current_node.value !== _elm) {
            current_node = current_node.next;
            if(current_node === null) return current_node
        }
        return current_node;
    }
    /**
     * 查找链表节点的上一个节点
     * @param {string} _elm 查找节点的value值
     * 
     */
    this.findPrev = function(_elm) {
        let current_node = this.head;
        while(!(current_node !== null && current_node.next.value === _elm)) {
            current_node = current_node.next;
            if(current_node.next === null && current_node.value !== _elm) return current_node.next
        }
        return current_node;
    }
    /**
     * 
     * 链表插入节点函数
     * @param {string} new_elm 新插入节点的value值
     * @param {string} _elm 被插入节点的value值
     * 
     */
    this.insert = function(new_elm,_elm) {
        const new_node = new Node(new_elm);
        const find_result = this.find(_elm);
        if(find_result !== null) {
            new_node.next = find_result.next;
            find_result.next = new_node;
        }else return new Error('该链表节点不存在')
    }
    /**
     *
     * 显示所有链表节点
     * 
     */
    this.show = function() {
        let current_node = this.head;
        while(current_node !== null) {
            console.log(current_node, "\t");
            current_node = current_node.next;
            if(current_node === null) return;
        }
    }
    /**
     * 链表删除链表节点函数
     * @param {string} _elm 删除节点的value值，用以匹配节点
     * 
     */
    this.remove = function(_elm){
        // 获取删除链表节点的上一节点
        const prev_node = this.findPrev(_elm)
        // 排除删掉表头和链表长度为二
        prev_node && (prev_node.next = (prev_node.next.next || null))
    }
}

const my_links = new link_list()
const find_result = my_links.insert('second node','head_node')
my_links.show()
my_links.remove('head node')
my_links.show()
/**
 * 
 */